<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="资产名称" size="small">
            <el-input v-model="searchForm.propertyName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="地点" size="small">
            <el-input v-model="searchForm.placeName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>

      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.placeId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="440px">
      <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="80px">
        <el-form-item prop="propertyName" label="资产名称" size="small">
          <el-input type="text" v-model="teamRow.propertyName"></el-input>
        </el-form-item>
        <el-form-item prop="propertyAmount" label="数量" size="small">
          <el-input type="number" v-model.number="teamRow.propertyAmount">
          </el-input>
        </el-form-item>
        <el-form-item prop="propertyNo" label="编号" size="small">
          <el-select v-model="teamRow.propertyNo" placeholder="请选择">
            <el-option
              v-for="item in propertyNoList"
              :key="item.propertyNo"
              :label="item.propertyNo"
              :value="item.propertyNo">
            </el-option>
          </el-select>
          <el-input type="text" v-if="teamRow.propertyNo" v-model="teamRow.propertyDigit"></el-input>
        </el-form-item>
        <el-form-item label="品牌" size="small">
          <el-input type="text" v-model="teamRow.brand"></el-input>
        </el-form-item>
        <el-form-item label="型号" size="small">
          <el-input type="text" v-model="teamRow.model"></el-input>
        </el-form-item>
        <el-form-item label="地点" size="small">
          <el-input type="text" v-model="teamRow.placeName"></el-input>
        </el-form-item>
        <el-form-item label="备注说明" size="small">
          <el-input type="textarea" v-model="teamRow.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
        <el-button size="small" @click="outerVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getList,
    insertAreaProperty,
    selectAreaProperty,
    delAreaProperty,
    updateAreaProperty,
    selectPropertyNo
  } from '@/api/zoneassets'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'

  const propertyNo = function(row, column) {
    return `${row.propertyNo}-${row.propertyDigit}`
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'propertyName', label: '资产名称', width: '150', fixed: true },
          { prop: 'propertyAmount', label: '数量', width: '150' },
          { prop: 'propertyNo', label: '编号', width: '150', formatter: propertyNo },
          { prop: 'brand', label: '品牌', width: '150' },
          { prop: 'model', label: '型号', width: '150' },
          { prop: 'placeName', label: '地点', width: '150' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        propertyNoList: [],
        tenderNames: [],
        infoTitles: [],
        listLoading: true,
        dialogTitle: '新增区域资产',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {
          propertyName: [
            { required: true, message: '请输入资产名称', trigger: 'change' }
          ],
          propertyAmount: [
            { required: true, message: '请输入数量', trigger: 'change' }
          ],
          propertyNo: [
            { required: true, message: '请选择编号', trigger: 'change' }
          ]
        },
        visible: false
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
      this.selectPropertyNo()
    },
    computed: {
      global() {
        return global
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        console.log(this.searchFormList)
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      selectPropertyNo() {
        selectPropertyNo().then(response => {
          this.propertyNoList = response.responseBody
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增区域资产'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.teamRow.isTender = 1
        this.dialogType = 1
      },
      editTeam(placeId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改区域资产'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          placeId: placeId
        }
        selectAreaProperty(params).then(response => {
          const data = response.responseBody
          this.teamRow = Object.assign({}, this.teamRow, data)
        })
      },
      submitForm(formName) {
        const params = {
          propertyName: this.teamRow.propertyName,
          propertyAmount: this.teamRow.propertyAmount,
          propertyNo: this.teamRow.propertyNo,
          propertyDigit: this.teamRow.propertyDigit ? this.teamRow.propertyDigit : '',
          brand: this.teamRow.brand ? this.teamRow.brand : '',
          model: this.teamRow.model ? this.teamRow.model : '',
          placeName: this.teamRow.placeName ? this.teamRow.placeName : '',
          remark: this.teamRow.remark ? this.teamRow.remark : ''
        }
        if (!this.dialogType) {
          params.placeId = this.teamRow.placeId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              insertAreaProperty(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateAreaProperty(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          placeId: scope.row.placeId,
          isDel: 1
        }
        delAreaProperty(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      selectGet(vId) {
        let obj = {}
        obj = this.GLOBAL.subjectionDepartmentList.find((item) => {
          return item.value === vId // 筛选出匹配数据
        })
        this.teamRow.inDepartment = obj.label
      },
      selectOut(vId) {
        let obj = {}
        obj = this.GLOBAL.subjectionDepartmentList.find((item) => {
          return item.value === vId // 筛选出匹配数据
        })
        this.teamRow.originalDepartment = obj.label
      }
    },
    mounted() {
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
